{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tree Level Order Print - SOLUTION\n",
    "\n",
    "Given a binary tree of integers, print it in level order. The output will contain space between the numbers in the same level, and new line between different levels. For example, if the tree is: \n",
    "___\n",
    "![title](tree_print.png)\n",
    "___\n",
    "The output should be: \n",
    "\n",
    "    1 \n",
    "    2 3 \n",
    "    4 5 6"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Solution\n",
    "\n",
    "It won’t be practical to solve this problem using recursion, because recursion is similar to depth first search, but what we need here is breadth first search. So we will use a queue as we did previously in breadth first search. First, we’ll push the root node into the queue. Then we start a while loop with the condition queue not being empty. Then, at each iteration we pop a node from the beginning of the queue and push its children to the end of the queue. Once we pop a node we print its value and space.\n",
    "\n",
    "To print the new line in correct place we should count the number of nodes at each level. We will have 2 counts, namely current level count and next level count. Current level count indicates how many nodes should be printed at this level before printing a new line. We decrement it every time we pop an element from the queue and print it. Once the current level count reaches zero we print a new line. Next level count contains the number of nodes in the next level, which will become the current level count after printing a new line. We count the number of nodes in the next level by counting the number of children of the nodes in the current level. Understanding the code is easier than its explanation:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class Node:\n",
    "    def __init__(self, val=None):\n",
    "        self.left = None\n",
    "        self.right = None\n",
    "        self.val =  val "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def levelOrderPrint(tree):\n",
    "    if not tree:\n",
    "        return\n",
    "    nodes=collections.deque([tree])\n",
    "    currentCount, nextCount = 1, 0\n",
    "    while len(nodes)!=0:\n",
    "        currentNode=nodes.popleft()\n",
    "        currentCount-=1\n",
    "        print currentNode.val,\n",
    "        if currentNode.left:\n",
    "            nodes.append(currentNode.left)\n",
    "            nextCount+=1\n",
    "        if currentNode.right:\n",
    "            nodes.append(currentNode.right)\n",
    "            nextCount+=1\n",
    "        if currentCount==0:\n",
    "            #finished printing current level\n",
    "            print '\\n',\n",
    "            currentCount, nextCount = nextCount, currentCount"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The time complexity of this solution is O(N), which is the number of nodes in the tree, so it’s optimal. Because we should visit each node at least once. The space complexity depends on maximum size of the queue at any point, which is the most number of nodes at one level. The worst case occurs when the tree is a complete binary tree, which means each level is completely filled with maximum number of nodes possible. In this case, the most number of nodes appear at the last level, which is (N+1)/2 where N is the total number of nodes. So the space complexity is also O(N). Which is also optimal while using a queue. \n",
    "\n",
    "Again, this is a very common tree interview question!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Good Job!"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
